<!--#include file="../Include/Conn.asp" -->
<!--#include file="../Include/config.asp" -->
<!--#include file="seeion.asp"-->
<%
dim action : action=getForm("action","get")
Select case action	
	case "add" : addSort
	case "Batch" : BatchAdd
	case "edit" : editSort
	case "del" : delSort
	case "saveall" : saveAll
	case "on" : onOff "on", "zych_Type", "SortID", "isok", "", getPageName()
	case "off" : onOff "off", "zych_Type", "SortID", "isok", "", getPageName()
	case "move" : moveSort
End Select

dim SortID,Page, ParentID, px_id, SortType, SortName,Sorthtml, SortURL,Sortimg, SortLevel, AddTime, PageTitle, PageKeywords, PageDesc, SortPath, isok, TopSortID, Content,Temp,sTemp,menu
dim sql, msg

Sub getSort
	dim id : id=getForm("id","get")
	if not isnul(ID) then		
		sql ="select * from [zych_Type] where SortID="&id
		dim rs : set rs = conn.execute(sql,"r1")
		if rs.eof then 
			alertMsgAndGo "没有这条记录","-1"
		else
			SortID=rs("SortID")
			ParentID=rs("ParentID")
			px_id=rs("px_id")
			SortType=rs("SortType")
			SortName=rs("SortName")
			Sorthtml=rs("Sorthtml")
			SortURL=rs("SortURL")
			Sortimg=rs("Sortimg")
			SortLevel=rs("SortLevel")
			AddTime=rs("AddTime")
			PageTitle=rs("PageTitle")
			PageKeywords=rs("PageKeywords")
			PageDesc=rs("PageDesc")
			SortPath=rs("SortPath")
			isok=rs("isok")
			menu=rs("menu")
			TopSortID=rs("TopSortID")
			Content=rs("SortContent")
			Page=rs("PageSize")
			Temp=rs("Temp")
			sTemp=rs("sTemp")
		end if
		rs.close : set rs=nothing
	else		
		alertMsgAndGo "没有这条记录","-1"
	end if 
End Sub

Sub moveSort
	dim id	:	id=getForm("id","post")
	if isnul(id) then alertMsgAndGo "请选择要操作的内容","-1"
	dim moveSortID 
	moveSortID=getForm("moveSortID","post")
	
	dim ids,i
	ids=split(id,",")
	for i=0 to ubound(ids)
		if moveSortID="0" then 
			SortLevel="1"
			TopSortID=ids(i)
			SortPath =TopSortID&","
		else
			dim rs 	: set rs=conn.execute("select SortLevel, TopSortID, SortPath from zych_Type where SortID="&moveSortID,"r1")
			SortLevel=rs(0)+1
			TopSortID=rs(1)
			SortPath=rs(2)&ids(i)&","
		end if		
		conn.execute"update zych_Type set ParentID="&moveSortID&", SortLevel="&SortLevel&", TopSortID="&TopSortID&", SortPath='"&trim(SortPath)&"' where SortID ="&ids(i), "exe"
		editSubSort(ids(i))
	next
		
	alertMsgAndGo "移动成功！", getPageName()
End Sub

Function editSubSort(sortID)	
	dim rs,prs,SortPath,SortLevel
	set rs=conn.execute("select * from zych_Type where parentID="&sortID,"r1")	
	if not rs.eof then		
		set prs=conn.execute("select * from zych_Type where SortID="&rs("parentID"),"r1")	
		do while not rs.eof			
			if rs("SortLevel")=1 then 
				SortPath = rs("SortID")&","
				conn.execute"update zych_Type set TopSortID="&rs("SortID")&",SortPath='"&SortPath&"' where SortID="&rs("SortID"),"exe"
			else
				SortPath =trim(prs("SortPath"))&rs("SortID")&","
				SortLevel=prs("SortLevel")+1
				conn.execute"update zych_Type set TopSortID="&prs("TopSortID")&", SortLevel="&SortLevel&", SortPath='"&SortPath&"' where SortID="&rs("SortID"),"exe"
			end if			
			editSubSort rs("SortID")			
			rs.movenext
		loop		
		prs.close : set prs=nothing
	end if
	rs.close : set rs=nothing
End Function 

Sub BatchAdd
	dim i,TopSortName,SubSortName
	AddTime=now()
	isok=1
	for i=1 to 10	
		ParentID=0
		TopSortName=getForm("TopSortName"&i, "post")		
		if not isnul(TopSortName) then 
			px_id=getForm("px_id"&i, "post")
			SortType=getForm("SortType"&i, "post")
			SubSortName=getForm("SubSortName"&i, "post")
			SortURL=getForm("SortURL"&i, "post")
			Page=20
			Temp="Temp1"
			conn.execute"insert into zych_Type(ParentID, px_id, SortType, SortName, SortURL, isok,PageSize,Temp, AddTime) values("&ParentID&", "&px_id&", "&SortType&", '"&TopSortName&"', '"&SortURL&"', "&isok&","&Page&", '"&Temp&"', '"&AddTime&"')", "exe"
			SortID=conn.execute("select @@identity","r1")(0)
			SortLevel="1"
			TopSortID=SortID
			SortPath = TopSortID&","			
			conn.execute"update zych_Type set SortLevel="&SortLevel&", TopSortID="&TopSortID&", SortPath='"&SortPath&"' where SortID="&SortID, "exe"
			
			ParentID=SortID
			if not isnul(SubSortName) then
				SubSortName=split(SubSortName,",")
				dim j
				for j=0 to ubound(SubSortName)	
					if not isnul(SubSortName(j)) then
						conn.execute"insert into zych_Type(ParentID, px_id, SortType, SortName, SortURL, isok,PageSize,Temp,AddTime) values("&ParentID&", "&px_id&", "&SortType&", '"&SubSortName(j)&"', '"&SortURL&"',  "&isok&","&Page&",'"&Temp&"','"&AddTime&"')", "exe"
					end if
				next
			end if		
			editSubSort(sortID)	
		end if	
	next	
	alertMsgAndGo "保存成功","admin_Class.asp"
End Sub

Sub saveAll
	if key<>0 and  instr(manage,"|11")=0 then
		Call adminJump("Sorry!","您没有管理该模块的权限！","javascript:window.history.go(-1)")
		response.End
	End if
	Dim ids				:	ids=split(getForm("SortIDs","post"),",")
	Dim SortNames		:	SortNames=split(getForm("SortNames","post"),",")
	Dim Sorthtmls		:	Sorthtmls=split(getForm("Sorthtml","post"),",")
	Dim SortURLs		:	SortURLs=split(getForm("SortURLs","post"),",")
	Dim px_ids		:	px_ids=split(getForm("px_ids","post"),",")
	If Ubound(ids)=-1 Then 	'防止有值为空时下标越界
		ReDim ids(0)
		ids(0)=""
	End If	
	If Ubound(SortNames)=-1 Then
		ReDim SortNames(0)
		SortNames(0)=""
	End If
	If Ubound(Sorthtmls)=-1 Then
		ReDim Sorthtmls(0)
		Sorthtmls(0)=""
	End If
	If Ubound(SortURLs)=-1 Then
		ReDim SortURLs(0)
		SortURLs(0)=""
	End If

	If Ubound(px_ids)=-1 Then
		ReDim px_ids(0)
		px_ids(0)=0
	End If
	Dim i
	For i=0 To Ubound(ids)		
		if not isnum(px_ids(i))  then px_ids(i)=0
		conn.execute "update zych_Type Set SortName='"&trim(SortNames(i))&"',Sorthtml='"&trim(Sorthtmls(i))&"',SortURL='"&trim(SortURLs(i))&"',px_id='"&trim(px_ids(i))&"' Where SortID="&trim(ids(i)),"exe"

	Next
	alertMsgAndGo "保存成功","admin_Class.asp"
End Sub	

function sortList(ParentID)
	set rs =conn.execute("select *,(select count(*) from zych_Type where ParentID=t.SortID) as c from zych_Type t where ParentID="&ParentID&" order by px_id,px_id ","r1")
	IF rs.eof or rs.bof Then
		echo "<tr bgcolor=""#ffffff"" align=""center""><td colspan=""9"">暂无记录！</td></tr>"
	Else
		Do While not rs.eof
		if rs("ParentID")<>0 then align="style=""width:40px;text-align:right""" else align="style=""width:40px"""
		if rs("Sortimg")<>"" then Sortimg="<img src="""&rs("Sortimg")&""" width=130 height=30 />" else Sortimg=""
			echo "<tr bgcolor=""#ffffff"" align=""center"" onMouseOver=""this.bgColor='#EEEEEE'"" onMouseOut=""this.bgColor='#ffffff'"">"&vbcrlf
			echo "<td height=""28"" class=""td""><input type=""checkbox"" name=""id"" class=""inp"" value="""&rs("SortID")&"""/><input type=""hidden"" name=""SortIDs"" class=""inp"" value="""&rs("SortID")&""" /></td>"&vbcrlf
			echo "<td class=""td""><input type=""text"" class=""inp"" style=""text-align:center;width:40px"" value="""&rs("SortID")&""" readonly/></td>"&vbcrlf
			echo "<td align=""left"" class=""td"">"&getLevel(rs("SortLevel"))&"<input name=""SortNames"" type=""text"" class=""inp"" id=""SortNames"" value="""&rs("SortName")&""" maxlength=""200"" style=""width:120px;"" class=""btn1""/>-<input name=""Sorthtml"" type=""text""  id=""Sorthtml"" class=""inp"" value="""&rs("Sorthtml")&"""  maxlength=""255"" style=""width:115px;"" class=""btn1""/></td>"&vbcrlf
			echo "<td class=""td"">"&Sortimg&"</td>"&vbcrlf
			echo "<td class=""td"">"&sortTypenames(rs("SortType"))&"</td>"&vbcrlf
			echo "<td class=""td"">"&rs("Temp")&"</td>"&vbcrlf
			echo "<td class=""td""><input name=""SortUrls"" type=""text"" class=""inp""  id=""SortUrls"" value="""&rs("SortUrl")&""" size=""18"" maxlength=""255""/></td>"&vbcrlf
			echo "<td class=""td"">"
			echo "<input name=""px_ids"" type=""text"" "&align&"  class=""inp"" id=""px_id"" value="""&rs("px_id")&""" size=""3"" maxlength=""4""/>"
			echo "</td>"&vbcrlf
			echo "<TD class=""td"">"&getStr(rs("isok"),"<a href=""?action=off&id="&rs("SortID")&""" title=""启用"" ><IMG src=""images/toolbar_ok.gif""></a>","<a href=""?action=on&id="&rs("SortID")&""" title=""禁用"" ><IMG src=""images/toolbar_no.gif""></a>")&"</TD>"&vbcrlf
			echo "<td class=""td""><input type=""button"" name=""Submit3"" value=""添加子类"" onClick=""window.location.href='add_Class.asp?id="&rs("Sortid")&"'""  class=""btn""/> <input type=""button"" name=""Submit3"" value=""修 改"" onClick=""window.location.href='Edit_Class.asp?id="&rs("Sortid")&"'""  class=""btn""/> <input type=""button"" name=""Submit"" value=""删 除"" onClick=""javascript:if(confirm('确定删除？删除后不可恢复!')){window.location.href='?action=del&id="&rs("Sortid")&"';}else{history.go(0);}""  class=""btn""/></td>"&vbcrlf			
			echo "</tr>"	&vbcrlf			
			if rs("c")>0 then SortList(rs("SortID"))			
			rs.MoveNext
		Loop
		rs.close : set rs = nothing
	End If
End Function

'添加栏目
Sub addSort	
	ParentID=getForm("ParentID", "post")
	px_id=getForm("px_id", "post")
	SortType=getForm("SortType", "post")
	SortName=getForm("SortName", "post")
	Sorthtml=getForm("Sorthtml", "post")
	SortURL=getForm("SortURL", "post")
	Sortimg=getForm("Sortimg", "post")
	PageTitle=getForm("PageTitle", "post")
	PageKeywords=getForm("PageKeywords", "post")
	PageDesc=getForm("PageDesc", "post")
	isok=getCheck(getForm("isok", "post"))
	menu=getCheck(getForm("menu", "post"))
	Content=Trim(Request.Form("Content"))
	Page=getForm("Page", "post")	
	Temp=getForm("Temp", "post")
	sTemp=getForm("sTemp", "post")		
	AddTime=date()
	if isnul(SortName) then alertMsgAndGo "分类名称不能为空","-1"
	if isnul(Temp) then alertMsgAndGo "频道模板不能为空","-1"
	if isnul(sTemp) then alertMsgAndGo "内页模板不能为空","-1"
	set rs=server.createobject("adodb.recordset")
	sql="select * from [zych_Type]"
	rs.open sql,conn,1,3
	rs.addnew
	rs("ParentID")=ParentID
	rs("px_id")=px_id
	rs("SortType")=SortType
	rs("SortName")=SortName
	rs("Sorthtml")=Sorthtml
	rs("SortURL")=SortURL
	rs("Sortimg")=Sortimg
	rs("PageTitle")=PageTitle
	rs("PageKeywords")=PageKeywords
	rs("PageDesc")=PageDesc
	rs("isok")=isok
	rs("menu")=menu
	rs("SortContent")=Content
	rs("PageSize")=Page
	rs("Temp")=Temp
	rs("sTemp")=sTemp
	rs("AddTime")=now()
	rs.update
	rs.close : set rs = nothing
		SortID=conn.execute("select @@identity","r1")(0)
	if ParentID="0" then 
		SortLevel="1"
		TopSortID=SortID
		SortPath = TopSortID&","
	else
		set rs=conn.execute("select SortLevel, TopSortID, SortPath from zych_Type where SortID="&ParentID,"r1")
		SortLevel=rs(0)+1
		TopSortID=rs(1)
		SortPath=rs(2)&SortID&","
	end if
	conn.execute "update zych_Type set SortLevel="&SortLevel&", TopSortID="&TopSortID&", SortPath='"&SortPath&"' where SortID="&SortID, "exe"	
	'alertMsgAndGo "添加成功，点击继续添加！","admin_Class.asp"
	alergo "添加成功，确定继续添加","add_Class.asp?id="&TopSortID&"","admin_Class.asp"
End Sub

'修改栏目
Sub editSort
	if key<>0 and  instr(manage,"|11")=0 then
		Call adminJump("Sorry!","您没有管理该模块的权限！","javascript:window.history.go(-1)")
		response.End
	End if	
	SortID=getForm("SortID", "post")
	ParentID=getForm("ParentID", "post")
	px_id=getForm("px_id", "post")
	SortType=getForm("SortType", "post")
	SortName=getForm("SortName", "post")
	Sorthtml=getForm("Sorthtml", "post")
	SortURL=getForm("SortURL", "post")
	Sortimg=getForm("Sortimg", "post")
	PageTitle=getForm("PageTitle", "post")
	PageKeywords=getForm("PageKeywords", "post")
	PageDesc=getForm("PageDesc", "post")
	isok=getCheck(getForm("isok", "post"))
	menu=getCheck(getForm("menu", "post"))
	Page=getForm("Page", "post")	
	Temp=getForm("Temp", "post")
	sTemp=getForm("sTemp", "post")	
	Content=Trim(Request.Form("Content"))	
	if isnul(SortName) then alertMsgAndGo "分类名称不能为空","-1"
	set rs=server.createobject("adodb.recordset")'生成所有内容
	sql="select * from [zych_Type] where SortID="&SortID
	rs.open sql,conn,1,3
	rs("ParentID")=ParentID
	rs("px_id")=px_id
	rs("SortName")=SortName
	rs("sorttype")=sorttype
	rs("Sorthtml")=Sorthtml
	rs("SortURL")=SortURL
	rs("Sortimg")=Sortimg
	rs("PageTitle")=PageTitle
	rs("PageKeywords")=PageKeywords
	rs("PageDesc")=PageDesc
	rs("isok")=isok
	rs("menu")=menu
	rs("SortContent")=Content
	rs("PageSize")=Page
	rs("Temp")=Temp
	rs("sTemp")=sTemp
	rs("AddTime")=now()
	rs.update
	rs.close : set rs = nothing
	
	if ParentID="0" then 
		SortLevel="1"
		TopSortID=SortID
		SortPath = TopSortID&","
	else
		dim rs 	: set rs=conn.execute("select SortLevel, TopSortID, SortPath from zych_Type where SortID="&ParentID,"r1")
		SortLevel=rs(0)+1
		TopSortID=rs(1)
		SortPath=rs(2)&SortID&","
	end if
	conn.execute "update zych_Type set SortLevel="&SortLevel&", TopSortID="&TopSortID&", SortPath='"&SortPath&"' where SortID="&SortID, "exe"
	conn.execute "update Content set SortType="&sorttype&", TopSortID="&TopSortID&" where SortID="&SortID, "exe"	
	editSubSort(SortID)
	alertMsgAndGo "修改成功","admin_Class.asp"
End Sub

Sub delSort
	if key<>0 and  instr(manage,"|11")=0 then
		Call adminJump("Sorry!","您没有管理该模块的权限！","javascript:window.history.go(-1)")
		response.End
	End if
	dim id :id=getForm("id","both")
	if isnul(id) then alertMsgAndGo "请选择要操作的内容","-1"
	dim ids,i
	ids=split(id,",")
	for i=0 to ubound(ids)
		if not isnul(ids(i)) then 
			dim subids : subids=getSubSort(ids(i), 1)
			dim subid : subid=split(subids,",")
			dim j
			if runmode=1 then
				for j=0 to ubound(subid)
					delList(trim(subid(j)))
				next
				dim rs, sql, filepath
				dim templateobj : set templateobj=new TemplateClass
				sql="select * from Content where SortID in ("&subids&")"
				sql="select Cid,Title,sortType,SortFolder,a.GroupID,a.AddTime,PageFileName,a.SortID,b.GroupID from Content as a, zych_Type as b where a.SortID=b.SortID and b.SortID in ("&subids&")"
				set rs=conn.execute(sql,"r1")		
				do while not rs.eof
					filepath=templateobj.getContentLink(rs("SortID"),rs("Cid"),rs("SortFolder"),rs("a.GroupID"),rs("AddTime"),rs("PageFileName"),rs("b.GroupID"))
					if isExistFile(filepath) then delFile filepath
					rs.movenext
				loop
			end if
			conn.execute"delete * from Content where SortID in ("&subids&")","exe"
			conn.execute"delete * from [zych_Type] where SortID in ("&subids&")","exe"
			'通过栏目ID获取字段列表后，通过字段名称逐一删除文章列表中的字段
			Dim rsObj:Set rsObj=conn.execute("select * from FieldSet where Fieldfl in ("&subids&") Order by FieldOrder Asc,FieldID","r1")
			If  rsObj.Eof Then 
				echo ""
			Else
			Do while not rsObj.Eof
				conn.execute("alter table Content drop column "&rsObj("Field_title"))
			rsObj.movenext
			Loop	
			end if
			rsObj.close:Set rsObj = nothing
			'以上结束，以下为同步删除字段表中的字段
			conn.execute"delete * from [FieldSet] where FieldFL in ("&subids&")","exe"
		end if
	next
	alertMsgAndGo "删除成功！",getPageName()		
End Sub
%>